<?php
	class authModel{

		private $auth = '';//当前管理员信息 private有属性，不能修改

		//构造函数-初始化——判断auth是否为空
		public function __construct(){
			if(isset($_SESSION['auth'])&&(!empty($_SESSION['auth']))){
				$this -> auth = $_SESSION['auth'];
			}
		}

		//进行登录验证的一系列业务逻辑
		public function loginsubmit(){
			if(empty($_POST['username'])||empty($_POST['password'])){
				return false;
			}
			$username = addslashes($_POST['username']);
			$password = addslashes($_POST['password']);
			//进行用户验证 checkuer
			if( $this -> auth = $this -> checkuser($username, $password) ){
				$_SESSION['auth'] = $this -> auth;
				return $this -> auth;
			}else{
				return $this -> auth;
			}
		}

		//登录  -  获取private的值
		public function getauth(){
			return $this -> auth;
		}

		//退出登录  -  清出private的值
		public function logout(){
			unset($_SESSION['auth']);
			$this -> auth = '';
		}

		//进行用户验证
		private function checkuser($username, $password){
			//preg_match("/[/](*?:[.php])/", $_SERVER['PHP_SELF'], $table);
			$table = strpos($_SERVER["REQUEST_URI"], 'admin')?admin:user;
			$adminobj = M('admin');
			$auth = $adminobj -> findOne_by_username($table, $username);
			if( (!empty($auth))&&$auth['password']==$password ){
				return $auth;
			}else{
				return false;
			}
		}


	}
?>